﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.MT3DMS.DomainLayer;

using HA.ModFlow.Infrastructure;
using HA.Data;
namespace HA.ModFlow.Controls.MT3DMSWindows
{
    /// <summary>
    /// winChemicalReation.xaml 的交互逻辑
    /// </summary>
    public partial class winChemicalReation : Window
    {
        public winChemicalReation( MT3DMSData mtdata ,Project proj)
        {
            InitializeComponent();
            project = proj;
            this.gcConcentration.Header = "吸附状态\n初始浓度\n[" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(mtdata.BasicTransport.ModelOption.Units) + "]";
            this.gcfAdsorption.Header = "第一吸附参数\n[" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetLength(mtdata.BasicTransport.ModelOption.Units.Length) + "^3/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetMass(mtdata.BasicTransport.ModelOption.Units.Mass) + "]";
            this.gcsAdsorption.Header = "第二吸附或\n双区域模型\n参数[1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(mtdata.BasicTransport.ModelOption.Units.Time) + "]";
            this.gcfDissolution.Header = "溶解状态的\n一阶反应率\n[1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(mtdata.BasicTransport.ModelOption.Units.Time) + "]"; ;
            this.gcAdsorptionRate.Header = "吸附状态的\n一阶反应率\n[1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(mtdata.BasicTransport.ModelOption.Units.Time) + "]"; ;
            this.tbAquifer.Text = "含水层介质的密度(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(mtdata.BasicTransport.ModelOption.Units) + "):";
            _pcChemical = new PCChemicalReaction(proj);

             this.DataContext = this._pcChemical;
             this._mdoption = mtdata.BasicTransport.ModelOption;
             PCChemicalReaction.mainWindow = this;
        }

        MTModelOption _mdoption;
        PCChemicalReaction _pcChemical;
        Project project;
        public PCChemicalReaction PcChemical
        {
            get { return _pcChemical; }
            set { _pcChemical = value; }
        }

        string WrapString(string source, int num)
        {
            string target = source;

            if (num <= 0)
                throw new Exception("数据无效，必须为正整数！");

            for (int i = source.Length - 1; i >= num - 1; )
            {
                if ((i + 1) % num == 0)
                {
                    if (i == source.Length - 1)
                    {
                        i -= num;
                        continue;
                    }
                    target = target.Insert(i + 1, "\n");
                    i -= num;
                }
                else
                    i -= (i + 1) % num;
            }

            return target;
        }
   

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            IsEnabledLstView();

          

        }

        private void IsEnabledLstView()
        {
            ISOTHMType iso = this._pcChemical.ChemicalReaction.IsoTHM;
            KineticRateReaction kin = this._pcChemical.ChemicalReaction.KineticRateReaction;
            if (kin == KineticRateReaction.NONE)
            {
                if (iso == ISOTHMType.NOSorption)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;

                    }
                }

                if (iso == ISOTHMType.LinearIsoTherm)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;

                    }
                }
                if (iso == ISOTHMType.FreundlichIsoTherm)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;

                    }
                }
                if (iso == ISOTHMType.LangmuirIsoTherm)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;

                    }
                }
                if (iso == ISOTHMType.FirstOrderKineticSorption)
                {
                    this.chkSpecifIC.IsEnabled = true;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        if (this.chkSpecifIC.IsChecked.Value)
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = true;
                        }
                        else
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        }
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;
                    }
                }
                if (iso == ISOTHMType.DualDomainTransferWithoutSorption)
                {
                    this.chkSpecifIC.IsEnabled = true;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        if (this.chkSpecifIC.IsChecked.Value)
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = true;
                        }
                        else
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        }
                        this._pcChemical.Species[i].EditSP1.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;
                    }
                }
                if (iso == ISOTHMType.DualDomainTransferWithSorption)
                {
                    this.chkSpecifIC.IsEnabled = true;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        if (this.chkSpecifIC.IsChecked.Value)
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = true;
                        }
                        else
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        }
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = false;
                    }
                }
            }
            else
            {
                if (iso == ISOTHMType.NOSorption)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
                if (iso == ISOTHMType.LinearIsoTherm)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = false;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
                if (iso == ISOTHMType.FreundlichIsoTherm)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
                if (iso == ISOTHMType.LangmuirIsoTherm)
                {
                    this.chkSpecifIC.IsEnabled = false;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
                if (iso == ISOTHMType.FirstOrderKineticSorption)
                {
                    this.chkSpecifIC.IsEnabled = true;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        if (this.chkSpecifIC.IsChecked.Value)
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = true;
                        }
                        else
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        }
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
                if (iso == ISOTHMType.DualDomainTransferWithoutSorption)
                {
                    this.chkSpecifIC.IsEnabled = true;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        if (this.chkSpecifIC.IsChecked.Value)
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = true;
                        }
                        else
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        }
                        this._pcChemical.Species[i].EditSP1.IsEnabled = false;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
                if (iso == ISOTHMType.DualDomainTransferWithSorption)
                {
                    this.chkSpecifIC.IsEnabled = true;
                    for (int i = 0; i < this._pcChemical.Species.Count; i++)
                    {
                        if (this.chkSpecifIC.IsChecked.Value)
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = true;
                        }
                        else
                        {
                            this._pcChemical.Species[i].EditIC.IsEnabled = false;
                        }
                        this._pcChemical.Species[i].EditSP1.IsEnabled = true;
                        this._pcChemical.Species[i].EditSP2.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC1.IsEnabled = true;
                        this._pcChemical.Species[i].EditRC2.IsEnabled = true;
                    }
                }
            }
            this.lstView.Items.Refresh();
        }
      

        private void cboIsoTHM_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.cboIsoTHM.SelectedIndex)
            {
                case 0:
                    this.btnBulkDensity.IsEnabled = false;
                    this.btnPorosity2.IsEnabled = false;  
                    break;
                case 1:
                     this.btnBulkDensity.IsEnabled = true ;
                    this.btnPorosity2.IsEnabled = false;
                    break;
                case 2:
                    this.btnBulkDensity.IsEnabled = true;
                    this.btnPorosity2.IsEnabled = false;
                    break;
                case 3:
                    this.btnBulkDensity.IsEnabled = true;
                    this.btnPorosity2.IsEnabled = false;
                    break;
                case 4:
                    this.btnBulkDensity.IsEnabled = true;
                    this.btnPorosity2.IsEnabled = false;
                    break;
                case 5:
                    this.btnBulkDensity.IsEnabled = false ;
                    this.btnPorosity2.IsEnabled = true;
                    break;
                case 6:
                    this.btnBulkDensity.IsEnabled = true;
                    this.btnPorosity2.IsEnabled = true;
                    break;
            }
            IsEnabledLstView();
        }

        private void btnBulkDensity_Click(object sender, RoutedEventArgs e)
        {
            //winBulkDensity win = new winBulkDensity(this._pcChemical.ChemicalReaction);
            //win.Owner = this;
            //win.ResizeMode = ResizeMode.NoResize;
            //win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //win.ShowDialog();

            ChemicalReaction cr=this._pcChemical.ChemicalReaction;

            List<double[,]> bulk = this._pcChemical.ChemicalReaction.BulkDensity;
            CellCenterDataSet3d ds = new CellCenterDataSet3d(bulk,null,cr.Nrows,cr.Ncols,cr.Nlayers);

            ds.Label = "密度";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(_mdoption.Units) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog()==true)
            {
                cr.SetBulkDensity(win.PcDs.Dataset.ToListData ());
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }



        }

        private void btnPorosity2_Click(object sender, RoutedEventArgs e)
        {
            //winImmobile win = new winImmobile(this._pcChemical.ChemicalReaction);
            //win.Owner = this;
            //win.ResizeMode = ResizeMode.NoResize;
            //win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //win.ShowDialog();


            ChemicalReaction cr = this._pcChemical.ChemicalReaction;

            List<double[,]> bulk = this._pcChemical.ChemicalReaction.Porosity2;
            CellCenterDataSet3d ds = new CellCenterDataSet3d(bulk, null, cr.Nrows, cr.Ncols, cr.Nlayers);
            ds.Label = "孔隙度";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds, project);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "无量纲";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {
                cr.SetPorosity2(win.PcDs.Dataset.ToListData());
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }

        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            this._pcChemical.Confirm();
          

            this.DialogResult = true;
            this.Close();
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IsEnabledLstView();
        }

        private void chkSpecifIC_Click(object sender, RoutedEventArgs e)
        {
            IsEnabledLstView();
        }
    }
}
